package com.cooldatasoft.domain.daoImpl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.cooldatasoft.domain.dao.UserProjectRolesDao;
import com.cooldatasoft.domain.model.Project;
import com.cooldatasoft.domain.model.Roles;
import com.cooldatasoft.domain.model.User;
import com.cooldatasoft.domain.model.UserProjectRoles;

public class UserProjectRolesDaoImpl extends GenericDaoImpl<UserProjectRoles, Long> implements UserProjectRolesDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<UserProjectRoles> getUserProjectRolesByIdUser(User idUser) {
		Criteria reservationCrit = getSession().createCriteria(UserProjectRoles.class);
		reservationCrit.add(Restrictions.eq("idUser", idUser));
		
		List<UserProjectRoles> res = reservationCrit.list();
		return res;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserProjectRoles> getUsersOnProject(Project idProject) {
		Criteria reservationCrit = getSession().createCriteria(UserProjectRoles.class);
		reservationCrit.add(Restrictions.eq("idProject", idProject));
		
		List<UserProjectRoles> res = reservationCrit.list();
		return res;
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserProjectRoles getUserProjectRolesByIdUserAndIdProject(
			Project projects, User user) {
		Criteria reservationCrit = getSession().createCriteria(UserProjectRoles.class);
		reservationCrit.add(Restrictions.eq("idUser", user));
		reservationCrit.add(Restrictions.eq("idProject", projects));
		
		List<UserProjectRoles> res = reservationCrit.list();
		return (res.size() > 0) ? res.get(0) : null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserProjectRoles> getUsersByRoleOnProject(Project projects,
			Roles role) {
		Criteria reservationCrit = getSession().createCriteria(UserProjectRoles.class);
		reservationCrit.add(Restrictions.eq("idRole", role));
		reservationCrit.add(Restrictions.eq("idProject", projects));
		
		List<UserProjectRoles> res = reservationCrit.list();
		return (res.size() > 0) ? res : null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<UserProjectRoles> getUsersByRole(Project project, Roles role) {
		Criteria reservationCrit = getSession().createCriteria(UserProjectRoles.class);
		reservationCrit.add(Restrictions.eq("idProject", project));
		reservationCrit.add(Restrictions.eq("idRole", role));
		
		List<UserProjectRoles> res = reservationCrit.list();
		return res;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserProjectRoles> getProjectsByUserWithRole(User user,
			Roles role) {
		Criteria reservationCrit = getSession().createCriteria(UserProjectRoles.class);
		reservationCrit.add(Restrictions.eq("idRole", role));
		reservationCrit.add(Restrictions.eq("idUser", user));
		
		List<UserProjectRoles> res = reservationCrit.list();
		return (res.size() > 0) ? res : null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserProjectRoles> getExistingRecord(Project project,
			User user, Roles role) {
		Criteria reservationCrit = getSession().createCriteria(UserProjectRoles.class);
		reservationCrit.add(Restrictions.eq("idRole", role));
		reservationCrit.add(Restrictions.eq("idUser", user));
		reservationCrit.add(Restrictions.eq("idProject", project));
		
		List<UserProjectRoles> res = reservationCrit.list();
		return (res.size() > 0) ? res : null;
	}

}
